package com.csd.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.csd.activity.PaymentActivity;
import com.csd.activity.R;
import com.csd.bean.Car;

public class ListSelectAdapter extends BaseAdapter {

	private List<Car> list;
	private Context context;
	
	
	public ListSelectAdapter(List<Car> list, Context context) {
		super();
		this.list = list;
		this.context = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View v = null;
		ViewHolder holder;
		if(convertView == null){
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.lookup_item, null);
			holder = new ViewHolder();
			TextView tv_variator = (TextView) v.findViewById(R.id.variator);
			TextView tv_row = (TextView) v.findViewById(R.id.row);
			TextView tv_seat = (TextView) v.findViewById(R.id.seat);
			TextView tv_describe = (TextView) v.findViewById(R.id.describe);
			TextView tv_carbrand = (TextView) v.findViewById(R.id.carBrand);
			TextView tv_plate = (TextView) v.findViewById(R.id.plate);
			ImageView img = (ImageView) v.findViewById(R.id.carImg);
			Button bt_order = (Button) v.findViewById(R.id.bt_order);
			Button bt_map = (Button) v.findViewById(R.id.bt_map);
			holder.tv_variator = tv_variator;
			holder.tv_row = tv_row;
			holder.tv_seat = tv_seat;
			holder.img = img;
			holder.tv_describe = tv_describe;
			holder.tv_carbrand = tv_carbrand;
			holder.tv_plate = tv_plate;
			holder.bt_order = bt_order;
			holder.bt_map = bt_map;
			v.setTag(holder);
		}else{
			v = convertView;
			holder = (ViewHolder) v.getTag();
		}
		Car car = list.get(position);
		holder.img.setImageResource(car.getImg());
		holder.tv_variator.setText(car.getVariator());
		holder.tv_row.setText(car.getRow()+"厢");
		holder.tv_seat.setText(car.getSeat()+"座");
		holder.tv_describe.setText(car.getDescribe());
		holder.tv_carbrand.setText(car.getCarBrand());
		holder.tv_plate.setText(car.getPlate());
		holder.bt_order.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(context, PaymentActivity.class);
				context.startActivity(intent);
			}
		});
		holder.bt_map.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
		return v;
	}

	private class ViewHolder{
		TextView tv_variator,tv_row,tv_seat,tv_describe,tv_carbrand,tv_plate;
		ImageView img;
		Button bt_order,bt_map;
	}
}
